﻿using System.Windows.Forms;
using System.Collections.Generic;
using System;

namespace CRC.Comun.Presentacion
{
    public abstract partial  class UsrExploradorFotos : UserControl
    {
        //MySql.Data.MySqlClient.MySqlDataAdapter ad;
        //DataAccess bd = new DataAccess();
        //DataSet ds = new DataSet();
        //private string strId = "";
        //private int cTb=-1;
        //private UsrThumbnail[] tB;
        //PictureBox tmpPic;

        protected ControladorGestionCategorias cGC = null;

        public UsrExploradorFotos()
        {
            InitializeComponent();
        }

        protected abstract void subirFoto(string pStrArchivo);

        private void btnSubirFoto_Click(object sender, System.EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK) 
            {
                subirFoto(openFileDialog1.FileName);
            }
        }

        private void butNuevaCateg_Click(object sender, System.EventArgs e)
        {
            FormCategoria nuevaCat = new FormCategoria("Nueva categoría");
            nuevaCat.StartPosition = FormStartPosition.CenterParent;

            if (nuevaCat.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    int intCat = cGC.nuevaCategoria(nuevaCat.TxtCateg.Text.Trim());
                    ListViewItem item = listView1.Items.Add(nuevaCat.TxtCateg.Text.Trim(), 0);
                    item.Tag = intCat;

                }
                catch (ApplicationException appEx)
                {
                    MessageBox.Show(appEx.Message, "Advertencia",
                        MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "Error",
                        MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        private void butElimCateg_Click(object sender, System.EventArgs e)
        {
            try
            {
                if (MessageBox.Show("¿Estás seguro de querer borrar la categoría seleccionada?",
                    "Confirmación", MessageBoxButtons.YesNo,
                    MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) == DialogResult.Yes
                )
                {
                    cGC.eliminarCategoria((int)listView1.SelectedItems[0].Tag);
                    listView1.Items.Remove(listView1.SelectedItems[0]);
                }
            }
            catch (ApplicationException appEx)
            {
                MessageBox.Show(appEx.Message, "Advertencia",
                    MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error",
                    MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        void listView1_AfterLabelEdit(object sender, System.Windows.Forms.LabelEditEventArgs e)
        {
            try
            {
                int intCat = (int)listView1.Items[e.Item].Tag;
                if (intCat != 1)
                {
                    cGC.renombrar(intCat, e.Label);
                }
                else
                {
                    e.CancelEdit = true;
                }
            }
            catch (ApplicationException appEx)
            {
                e.CancelEdit = true;
                MessageBox.Show(appEx.Message, "Advertencia",
                    MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            catch (Exception ex)
            {
                e.CancelEdit = true;
                MessageBox.Show(ex.Message, "Error",
                    MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        protected void mostrarCargando(Boolean pMostrar)
        {
            lblCargandoFotos.Visible = pMostrar;
            if (pMostrar) panelMiniaturas.Controls.Add(lblCargandoFotos);
            else panelMiniaturas.Controls.Remove(lblCargandoFotos);
        }

        private void btnCerrar_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        //private void btnImprimir_Click(object sender, EventArgs e)
        //{                
        //    printDialog1.Document = printDocument1;

        //    if ((printDialog1.ShowDialog() == DialogResult.OK))
        //    {
        //        //printDocument1.PrintPage += new PrintPageEventHandler(this.OnPrintPage);
        //        printDocument1.Print();
        //    }
        //}

        //private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        //{

        //  // String textToPrint = "Imagen" + tB[cTb].Name;
        //  // Font printFont = new Font("Courier New", 12);
          
        //  // e.Graphics.DrawString(textToPrint, printFont, Brushes.Black, 0, 0);
           
        //   Point mp = new Point(25,20);
        //   e.Graphics.DrawImage(tmpPic.Image, mp);
        //}

    }
}
